﻿// <copyright file="BooleanExtensionsTest.cs" company="DotNetLib">
//
// .netlib: http://www.codeplex.com/DotNetLib
//
// This code is released under the New BSD License.
// A copy of this license is available at:
// http://www.codeplex.com/DotNetLib/license
//
// </copyright>

namespace DotNetLib.Tests
{
  using System;
  using System.Text;
  using DotNetLib.Text;
  using global::Microsoft.VisualStudio.TestTools.UnitTesting;

  [TestClass()]
  public class BooleanExtensionsTest
  {
    [TestMethod()]
    public void TestToString()
    {
      Assert.AreEqual("Yes", true.ToString("yn"));
      Assert.AreEqual("No", false.ToString("yn"));
    }

    [TestMethod()]
    public void TestStringFormat()
    {
      Assert.AreEqual("Yes", string.Format(new BooleanFormatProvider(), "{0:yn}", true));
      Assert.AreEqual("No", string.Format(new BooleanFormatProvider(), "{0:yn}", false));

      Assert.AreEqual("Good", string.Format(new BooleanFormatProvider(), "{0:Good|Bad}", true));
      Assert.AreEqual("Bad", string.Format(new BooleanFormatProvider(), "{0:Good|Bad}", false));

      Assert.AreEqual(Boolean.TrueString, string.Format(new BooleanFormatProvider(), "{0}", true));
      Assert.AreEqual(Boolean.FalseString, string.Format(new BooleanFormatProvider(), "{0}", false));
    }

    [TestMethod()]
    public void TestStringBuilderFormat()
    {
      StringBuilder builder = new StringBuilder();
      builder.AppendFormat(new BooleanFormatProvider(), "{0:yn} {1:yn}", true, false);
      builder.AppendFormatLine(new BooleanFormatProvider(), "{0:yn}", true);
      Assert.AreEqual("Yes NoYes\r\n", builder.ToString());
    }

    [TestMethod()]
    public void TestGetFormat()
    {
      BooleanFormatProvider provider = new BooleanFormatProvider();
      Assert.IsInstanceOfType(provider.GetFormat(typeof(ICustomFormatter)), typeof(BooleanFormatProvider));
      Assert.IsNull(provider.GetFormat(typeof(string)));
    }

    [TestMethod()]
    [ExpectedException(typeof(ArgumentException))]
    public void TestFormat_Exception1()
    {
      BooleanFormatProvider provider = new BooleanFormatProvider();
      provider.Format("yn", "true", provider);
    }

    [TestMethod()]
    [ExpectedException(typeof(FormatException))]
    public void TestFormat_Exception2()
    {
      BooleanFormatProvider provider = new BooleanFormatProvider();
      provider.Format("gb", true, provider);
    }

    [TestMethod()]
    [ExpectedException(typeof(FormatException))]
    public void TestFormat_Exception3()
    {
      BooleanFormatProvider provider = new BooleanFormatProvider();
      provider.Format("true|false|otherwise", true, provider);
    }
  }
}
